<?php
    
    function viewTASetting(){
        global $user;
        define("REQ_LEVEL",9);
        if($user->level < REQ_LEVEL){
            Page::$content=UN_AUTH;
            return; 
        }
        global $database;

        $url = "./ajax/tasetting.php";
        $baseLinkUrl = "agricare.php";
        $addParamE = "&page=edittasetting";
        $addParamD = "&page=deltasetting";
        $idName = "id";

        $str="<link rel='stylesheet' type='text/css' media='screen' href='./css/redmond/jquery-ui-1.7.1.custom.css' />
            <link rel='stylesheet' type='text/css' media='screen' href='./css/ui.jqgrid.css' />
            <script src='./js/jquery.js' type='text/javascript'></script>
            <script src='./js/i18n/grid.locale-en.js' type='text/javascript'></script>
            <script src='./js/jquery.jqGrid.min.js' type='text/javascript'></script>
            
            <script type='text/javascript'>
            jQuery(document).ready(function(){

            jQuery('#list').jqGrid({
                url:'{$url}',
                datatype: 'xml',
                mtype: 'GET',
                colNames:['SN','Username', 'Amount / km. (Rs.)','Date','-','-'],
                colModel :[ 
                {name:'id', index:'id', width:25,sortable:false}, 
                {name:'username', index:'username', width:120, align:'left',sorttype:'text'}, 
                {name:'amount', index:'amount', width:80, align:'left',sortable:false}, 
                {name:'date', index:'date', width:60, align:'left',sortable:false}, 
                {name:'a', index:'a', width:20, align:'center', sortable:false, formatter:'showlink', formatoptions:{baseLinkUrl:'{$baseLinkUrl}', addParam: '{$addParamE}', idName:'{$idName}'}}, 
                {name:'b', index:'b', width:20, align:'center', sortable:false, formatter:'showlink', formatoptions:{baseLinkUrl:'{$baseLinkUrl}', addParam: '{$addParamD}', idName:'{$idName}'}} 
                ],
                pager: '#pager',
                rowNum:8,
                    width:500,
                    height:175,
                    hidegrid:false,
                rowList:[10,20,30],
                sortname: 'username',
                sortorder: '',
                viewrecords: true,
                imgpath: '',
                caption: 'TA Setting List'
            }); 
            }); 
            </script>
            <table id='list'></table> 
            <div id='pager'></div> 
            ";

            //echo $str."dfjsdjfksdfjs";
            //die();
            Page::$content = $str;

    }

    function addTASetting(){
        global $user;
        define("REQ_LEVEL",9);
        if($user->level < REQ_LEVEL){
            Page::$content=UN_AUTH;
            return; 
        }
        global $form,$ctrl,$database,$page;
        $str;

        if($_SESSION['addtaSuccess']){
                unset($_SESSION['addtaSuccess']);
                $str.= "<div class='notice'>Data Entered Successfully !!</div>";
        }
                

        if(isset($_POST['add'])){
            //print_r($_POST);
            //die();
            $retval = $ctrl->addTASetting($_POST);


            if($retval){
                $_SESSION['addtaSuccess'] = true;
                header("Location:".$ctrl->referrer);				
            }
            else{
                $_SESSION['value_array'] = $_POST;
                $_SESSION['error_array'] = $form->getErrorArray();
                header("Location:".$ctrl->referrer);				
            }


        }else{


            Page::$jslink=array('js/ui.core.js','js/ui.datepicker.js');
            Page::$csslink=array('css/ui.all.css');
            $userList = $database->getUserList();

            $str.="<div id= 'heading'><h3>Add TA Settings</h3></div><div><form method='POST' action='{$_SERVER['REQUEST_URI']}'>
                <div>username : <select name='user_id'> ";
            
            foreach($userList as $key){
                $selected="";
                if($key['id'] == $form->value('user_id'))
                    $selected = "selected";
                $str.= "<option value='{$key['id']}' {$selected}>{$key['username']}</option>";
            }
            
            $str.= "</select></div>
                    <div>Amount : <input type='text' name='amount' value=\"{$form->value('amount')}\" />{$form->error('amount')}</div>
                    <div>Effective Date : <input type='text' name='effective_date' id='datepicker' value=\"{$form->value('effective_date')}\" />{$form->error('effective_date')}</div>
                    <div><input type='hidden' value='1' name='add'></div>
                    <div><input type='submit' value='Add' name='submit'></div>
                    </form></div>
                    
            <script type='text/javascript'>
            $(document).ready(function(){
                                        $('#datepicker').datepicker({dateFormat:'yy-mm-dd'});
                                        });
            </script>";
                
       Page::$content=$str;
       
            //return $str;

        }
    }

        function editTASetting(){

        global $form,$ctrl,$database,$page;
        $str;

        if($_SESSION['edittaSuccess']){
                unset($_SESSION['edittaSuccess']);
                $str.= "<div class='notice'>Data Updated Successfully !!</div>";
        }
                

        if(isset($_POST['edit'])){
            //print_r($_POST);
            //die();
            $retval = $ctrl->editTASetting($_POST);


            if($retval){
                $_SESSION['edittaSuccess'] = true;
                header("Location:".$ctrl->referrer);				
            }
            else{
                $_SESSION['value_array'] = $_POST;
                $_SESSION['error_array'] = $form->getErrorArray();
                header("Location:".$ctrl->referrer);				
            }


        }else{


            Page::$jslink=array('js/ui.core.js','js/ui.datepicker.js');
            Page::$csslink=array('css/ui.all.css');
            $data = $database->getTASettingById($_GET['id']);
            $userList = $database->getUserList();
            

            $form->setValue('user_id',$data['user_id']);            
            $form->setValue('amount',$data['amount']);          
            $form->setValue('effective_date',$data['effective_date']);
            $form->setValue('id',$data['id']);          
            //echo $form->value('user_id');
                $str.="<div><p>Edit TA Settings</p></div><div><form method='POST' action='{$_SERVER['REQUEST_URI']}'>
                <div>username : <select name='user_id'> ";
           //print_r($userList); 
            foreach($userList as $key){
                $selected ="";
                if($key['id'] == $form->value('user_id'))
                    $selected = "selected";
                $str.= "<option value='{$key['id']}' {$selected}>{$key['username']}</option>";
            }
            
            $str.= "</select></div>
                    <div>Amount : <input type='text' name='amount' value=\"{$form->value('amount')}\" />{$form->error('amount')}</div>
                    <div>Effective Date : <input type='text' name='effective_date' id='datepicker' value=\"{$form->value('effective_date')}\" />{$form->error('effective_date')}</div>
                    <div><input type='hidden' value=\"{$form->value('id')}\" name='id'></div>
                    <div><input type='hidden' value='1' name='edit'></div>
                    <div><input type='submit' value='Edit' name='submit'></div>
                
                    </form></div>
                    
            <script type='text/javascript'>
            $(document).ready(function(){
                                        $('#datepicker').datepicker({dateFormat:'yy-mm-dd'});
                                        });
            </script>";
                
       Page::$content=$str;
       
            //return $str;

        }
    }

?>
